<?php
namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\func\func;
use api\modules\v1\models\User;
use yii\web\HttpException;
use yii\web\ServerErrorHttpException;
use Yii;

class ShieldController extends U
{
    public $modelClass = 'api\modules\v1\models\Shield';

    public function behaviors() {
        if(in_array(Yii::$app->controller->action->id,['userinfo'])){ //排除评论列表权限验证
            $this->_is_auth = false;
        }
        return parent::behaviors();
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }

    /*
     * 屏蔽信息
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionCreate(){
        $model = new $this->modelClass();
        $model->attributes = Yii::$app->request->post();
        $model->uid = Yii::$app->user->identity->id;
        if($model->shield()){
            return [
                "message"=>"屏蔽成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 获取单个用户信息
     */
    public function actionUserinfo($id){
        $id = intval($id);
        if($user = User::find()->select(['id','nickname','phone'])->where(['id'=>$id])->asArray()->one()){
            $user['nickname'] = $user['nickname'] !='' ? $user['nickname'] : func::replace_phone($user['phone']);
            return $user;
        }
        return (object)[];
    }



}